#******************************************************************
#
# Copyright 2015 Samsung Electronics All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

######################################################################
#  Resource-encapsulation build script
######################################################################
import platform
Import('env')

SConscript('src/common/SConscript')
SConscript('src/serverBuilder/SConscript')

# Add third party libraries
lib_env = env.Clone()
SConscript('#service/third_party_libs.scons', 'lib_env')

resourceClient_env = lib_env.Clone()

######################################################################
#building Resource client, resourceBroker and resourceCache
######################################################################

if resourceClient_env.get('RELEASE'):
    resourceClient_env.AppendUnique(CCFLAGS=['-Os'])
else:
    resourceClient_env.AppendUnique(CCFLAGS=['-g'])

target_os = env.get('TARGET_OS')

if target_os in ['ios']:
    resourceClient_env.AppendUnique(CPPDEFINES=['TARGET_OS_IPHONE'])

######################################################################
# Build flags
######################################################################
resourceClient_env.AppendUnique(CPPPATH=[
    'include',
    '#/resource/include',
    '#/resource/c_common',
    '#/resource/c_common/oic_malloc/include',
    '#/resource/c_common/oic_string/include',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/oc_logger/include',
    'src/common/primitiveResource/include',
    'src/common/expiryTimer/include',
    'src/common/utils/include',
    'src/resourceBroker/include',
    'src/resourceCache/include'
])

resourceClient_env.PrependUnique(
    LIBS=['c_common', 'oc', 'rcs_common', 'octbstack', 'oc_logger', 'logger'])

if target_os not in ['windows']:
    resourceClient_env.AppendUnique(CXXFLAGS=['-Wall', '-std=c++0x'])
    resourceClient_env.AppendUnique(LIBS=['dl'])
    resourceClient_env.AddPthreadIfNeeded()

if target_os in ['android']:
    resourceClient_env.AppendUnique(CXXFLAGS=['-frtti', '-fexceptions'])
    resourceClient_env.PrependUnique(LIBS=['gnustl_shared', 'log'])
    resourceClient_env.AppendUnique(LINKFLAGS=['-Wl,-soname,librcs_client.so'])

######################################################################
# Source files and Targets
######################################################################
BROKER_SRC_DIR = 'src/resourceBroker/src/'
CACHE_SRC_DIR = 'src/resourceCache/src/'
RESOURCECLIENT_DIR = 'src/resourceClient/'

client_src = [
    BROKER_SRC_DIR + 'DeviceAssociation.cpp',
    BROKER_SRC_DIR + 'DevicePresence.cpp',
    BROKER_SRC_DIR + 'ResourcePresence.cpp',
    BROKER_SRC_DIR + 'ResourceBroker.cpp', CACHE_SRC_DIR + 'DataCache.cpp',
    CACHE_SRC_DIR + 'ResourceCacheManager.cpp',
    CACHE_SRC_DIR + 'ObserveCache.cpp',
    RESOURCECLIENT_DIR + 'RCSDiscoveryManager.cpp',
    RESOURCECLIENT_DIR + 'RCSDiscoveryManagerImpl.cpp',
    RESOURCECLIENT_DIR + 'RCSRemoteResourceObject.cpp'
]
ResourceClientsdk_static = resourceClient_env.StaticLibrary(
    'rcs_client', client_src)
if target_os not in ['ios']:
    ResourceClientsdk_shared = resourceClient_env.SharedLibrary(
        'rcs_client', client_src)
    resourceClient_env.InstallTarget(
        [ResourceClientsdk_static, ResourceClientsdk_shared], 'rcs_client')
    resourceClient_env.UserInstallTargetLib(
        [ResourceClientsdk_static, ResourceClientsdk_shared], 'rcs_client')
else:
    resourceClient_env.InstallTarget([ResourceClientsdk_static], 'rcs_client')
    resourceClient_env.UserInstallTargetLib([ResourceClientsdk_static],
                                            'rcs_client')
resourceClient_env.UserInstallTargetHeader(
    'include/RCSAddress.h', 'service/resource-encapsulation', 'RCSAddress.h')
resourceClient_env.UserInstallTargetHeader('include/RCSDiscoveryManager.h',
                                           'service/resource-encapsulation',
                                           'RCSDiscoveryManager.h')
resourceClient_env.UserInstallTargetHeader('include/RCSRemoteResourceObject.h',
                                           'service/resource-encapsulation',
                                           'RCSRemoteResourceObject.h')
resourceClient_env.UserInstallTargetHeader('include/RCSResourceAttributes.h',
                                           'service/resource-encapsulation',
                                           'RCSResourceAttributes.h')
resourceClient_env.UserInstallTargetHeader('include/RCSResourceObject.h',
                                           'service/resource-encapsulation',
                                           'RCSResourceObject.h')
resourceClient_env.UserInstallTargetHeader('include/RCSException.h',
                                           'service/resource-encapsulation',
                                           'RCSException.h')
resourceClient_env.UserInstallTargetHeader(
    'include/RCSRequest.h', 'service/resource-encapsulation', 'RCSRequest.h')
resourceClient_env.UserInstallTargetHeader(
    'include/RCSResponse.h', 'service/resource-encapsulation', 'RCSResponse.h')
resourceClient_env.UserInstallTargetHeader('include/RCSRepresentation.h',
                                           'service/resource-encapsulation',
                                           'RCSRepresentation.h')
resourceClient_env.UserInstallTargetHeader('include/RCSSeparateResponse.h',
                                           'service/resource-encapsulation',
                                           'RCSSeparateResponse.h')

######################################################################
# Build Sample App: SampleResourceClient & SampleResourceServer
######################################################################
SConscript('examples/SConscript')

######################################################################
# Build UnitTests Resource Client , resourceCache and resourceBroker and
# DiscoveryManager
################################################ ######################
if resourceClient_env.get('WITH_TEST') and target_os in ('linux'):
    SConscript('unittests/SConscript')
    SConscript('src/resourceCache/unittests/SConscript')
    SConscript('src/resourceBroker/unittest/SConscript')

if target_os == 'android':
    SConscript('android/SConscript')
